package com.neuedu.spring.jdbc.dao;

import com.neuedu.spring.jdbc.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 项目：      spring-framework
 * 类名:       UserDao
 * 创建时间：  2024/3/14 08：49
 * 描述 :
 * 作者 :     张金山
 * QQ :     314649444
 * Site:      https://jshand.gitee.io
 */
@Repository
public class UserDao {


    @Autowired
    JdbcTemplate jdbcTemplate;


    /**
     * 查询集合
     * @return
     */
    public List<Map<String ,Object>> selectListMap(){

        String sql = "select * from t_user ";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);

        return maps;
    }

    /**
     * 查询集合
     * @return
     */
    public List<User> selectListEntity(){
        String sql = "select * from t_user ";
        List<User> userList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<User>(User.class));
        return userList;
    }


    public User selectByIdEntiry(int id) {

        String sql = "select * from t_user where user_id = ? ";
        User user = jdbcTemplate.queryForObject(sql, new Object[]{id}, new BeanPropertyRowMapper<User>(User.class));
        return user;
    }
    /**
     * 根据主键查询单条结果
     * @return
     */
    public User selectById(int id){

        String sql = "select * from t_user where user_id = ? ";

//        Map<String, Object> row = jdbcTemplate.queryForMap(sql, id);
//        System.out.println("row = " + row);

        User user  = jdbcTemplate.queryForObject(sql, new Object[]{id}, new RowMapper<User>() {
            @Override
            public User mapRow(ResultSet resultSet, int i) throws SQLException {
                int userId = resultSet.getInt("user_id");
                String userName = resultSet.getString("user_name");
                String gender = resultSet.getString("gender");
                Date birthday = resultSet.getTimestamp("birthday");
                int status = resultSet.getInt("status");



                User user = new User();
                user.setUserId(userId);
                user.setUserName(userName);
                user.setGender(gender);
                user.setBirthday(birthday);
                user.setStatus(status);
                return user;
            }
        });


        return user;
    }

    /**
     * 根据主键查询单条结果
     * @return
     */
    public int selectCount(){

        String sql = "select count(*) from   t_user ";
        Integer count = jdbcTemplate.queryForObject(sql, Integer.class);
        return count;
    }


    public int insert(User user){

        String sql = "insert into t_user(user_id,user_name,gender,birthday,status) values(?,?,?,?,?)";
        return jdbcTemplate.update(sql,
                user.getUserId(),
                user.getUserName(),
                user.getGender(),
                user.getBirthday(),
                user.getStatus()
                );
    }

    public int updateById(User user){
        StringBuffer sql = new StringBuffer();
        sql.append(" update 					");
        sql.append("   `t_user`  set				");
        sql.append(" `user_name` = ?,				");
        sql.append(" `gender` = ?,				");
        sql.append(" `birthday` = ?,				");
        sql.append(" `status` = ? where `user_id` = ?		");

        return jdbcTemplate.update(sql.toString(),    user.getUserName(),user.getGender(),user.getBirthday(),user.getStatus(),user.getUserId()  );

    }


    public int deleteById(int id){
        String sql = "delete from t_user where user_id = ?";
        return jdbcTemplate.update(sql,    id   );
    }





}
